/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainJFrame.java
 *
 * Created on 31/05/2011, 00:26:40
 */
package main;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import view.BasePanelControl;
import view.InitPanel;
import view.Progress;
import view.ProjeteisPanelView;
import view.Space3DPanelView;

/**
 *
 * @author mathyas
 */

/*
 * Este é o frame que gerencia o programa como um todo.
 * Ele simplesmente trata os eventos dos menus.
 * A idéia é que quando clicado para abrir outro fenômeno,o antigo fenômeno seja fechado.
 * Os seguintes passos devem ser seguidos:
 * 1. Teste se o fenômeno que deseja ser aberto já está aberto, caso sim, informe ao usuário.
 * 2. Uma thread exibe uma barra de progresso (Progress).
 * 3. Outra thread carrega o painel que exibirá o novo fenômeno com jMonkey.
 * 4. Após o painel carregar, feche a barra de progresso.
 */
public class MainJFrame extends javax.swing.JFrame {

    private BasePanelControl panel;
    private Progress progress;

    /** Creates new form MainJFrame */
    public MainJFrame() {
        initComponents();
        panel = new InitPanel();  //polimorfismo
        add(panel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupFenomeno = new javax.swing.ButtonGroup();
        buttonGroupAparencia = new javax.swing.ButtonGroup();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuFenomenos = new javax.swing.JMenu();
        jMenuEspaco = new javax.swing.JMenu();
        jRadioButtonMenuItemEspaco3D = new javax.swing.JRadioButtonMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();
        jMenuOpcoes = new javax.swing.JMenu();
        jMenuAparencia = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
        jMenuAjuda = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LabFis - Laboratório Virtual de Física");
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("frameMain"); // NOI18N

        jMenuFenomenos.setText("Fenômenos");

        jMenuEspaco.setText("Espaço");

        buttonGroupFenomeno.add(jRadioButtonMenuItemEspaco3D);
        jRadioButtonMenuItemEspaco3D.setText("Espaço 3-D");
        jRadioButtonMenuItemEspaco3D.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItemEspaco3DActionPerformed(evt);
            }
        });
        jMenuEspaco.add(jRadioButtonMenuItemEspaco3D);

        jMenuFenomenos.add(jMenuEspaco);

        jMenu1.setText("Mecânica");

        buttonGroupFenomeno.add(jRadioButtonMenuItem4);
        jRadioButtonMenuItem4.setText("Projéteis");
        jRadioButtonMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jRadioButtonMenuItem4);

        jMenuFenomenos.add(jMenu1);

        jMenuBar1.add(jMenuFenomenos);

        jMenuOpcoes.setText("Opções");

        jMenuAparencia.setText("Aparência");

        buttonGroupAparencia.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("Java");
        jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        jMenuAparencia.add(jRadioButtonMenuItem1);

        buttonGroupAparencia.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setText("Nimbus");
        jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        jMenuAparencia.add(jRadioButtonMenuItem2);

        buttonGroupAparencia.add(jRadioButtonMenuItem3);
        jRadioButtonMenuItem3.setText("Windows");
        jRadioButtonMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem3ActionPerformed(evt);
            }
        });
        jMenuAparencia.add(jRadioButtonMenuItem3);

        jMenuOpcoes.add(jMenuAparencia);

        jMenuBar1.add(jMenuOpcoes);

        jMenuAjuda.setText("Ajuda");

        jMenuItem1.setText("Sobre");
        jMenuAjuda.add(jMenuItem1);

        jMenuBar1.add(jMenuAjuda);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButtonMenuItemEspaco3DActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItemEspaco3DActionPerformed

        if(panel instanceof Space3DPanelView) {
            String msg = "Este fenômeno já esta aberto.";
            JOptionPane.showMessageDialog(MainJFrame.this, msg, "Fenômeno Aberto", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        progress = new Progress(this, true);
        SwingUtilities.invokeLater(progress);

        Thread t = new Thread(new Runnable() {

            public void run() {
                if (panel != null) {
                    remove(panel);
                    panel.destroy();
                }
                panel = new Space3DPanelView();
                panel.start();
                add(panel);
                SwingUtilities.updateComponentTreeUI(panel);
                progress.dispose();
            }
        });
        t.start();


    }//GEN-LAST:event_jRadioButtonMenuItemEspaco3DActionPerformed

    private void jRadioButtonMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem3ActionPerformed
        try {
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
        } catch(UnsupportedLookAndFeelException ex) {
            String msg = "Aparência não disponível";
            JOptionPane.showMessageDialog(this, msg, "Falha", JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_jRadioButtonMenuItem3ActionPerformed

    private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch(UnsupportedLookAndFeelException ex) {
            String msg = "Aparência não disponível";
            JOptionPane.showMessageDialog(this, msg, "Falha", JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

    private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch(UnsupportedLookAndFeelException ex) {
            String msg = "Aparência não disponível";
            JOptionPane.showMessageDialog(this, msg, "Falha", JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

    private void jRadioButtonMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem4ActionPerformed
        if(panel instanceof ProjeteisPanelView) {
            String msg = "Este fenômeno já esta aberto.";
            JOptionPane.showMessageDialog(MainJFrame.this, msg, "Fenômeno Aberto", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        progress = new Progress(this, true);
        SwingUtilities.invokeLater(progress);

        Thread t = new Thread(new Runnable() {

            public void run() {
                if (panel != null) {
                    remove(panel);
                    panel.destroy();
                }
                panel = new ProjeteisPanelView();
                panel.start();
                add(panel);
                SwingUtilities.updateComponentTreeUI(panel);
                progress.dispose();
            }
        });
        t.start();
    }//GEN-LAST:event_jRadioButtonMenuItem4ActionPerformed
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        Progress progress = new Progress(null, true);
        SwingUtilities.invokeLater(progress);

        MainJFrame main = new MainJFrame();
        main.setLocationRelativeTo(null);
        progress.dispose();
        main.setVisible(true);


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupAparencia;
    private javax.swing.ButtonGroup buttonGroupFenomeno;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenuAjuda;
    private javax.swing.JMenu jMenuAparencia;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuEspaco;
    private javax.swing.JMenu jMenuFenomenos;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenu jMenuOpcoes;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItemEspaco3D;
    // End of variables declaration//GEN-END:variables
}
